package com.example.villagemarket.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.example.villagemarket.R;
import com.example.villagemarket.ShoppingCart;

import com.haarman.listviewanimations.itemmanipulation.ExpandableListItemAdapter;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

public class MenuAdapter extends ExpandableListItemAdapter<HashMap<String, String>> {

	private Activity activity;
	private List<HashMap<String, String>> data;
	private static LayoutInflater inflater = null;
	Context context;
	HashMap<String, String> merchandise;
	
	//Holds items to add to the shopping cart
	ArrayList<String> tempHolderForShoppingCart;
	
	//Shopping cart object to manage the shopping cart
	ShoppingCart sc = new ShoppingCart();
		
	
	public MenuAdapter(Context c, List<HashMap<String, String>> items, Activity a) {
		super(c, items);
		
		activity = a;
		data = items;
		inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		context = c;
		
		merchandise = new HashMap<String, String>();
		
		setLimit(1);
		
		tempHolderForShoppingCart = new ArrayList<String>();
		
	}






	public int getCount() {
		return data.size();
	}

	public HashMap<String, String> getItem(HashMap<String, String> position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

    @Override
    public View getTitleView(int position, View convertView, ViewGroup parent) {
    	View vi = convertView;
		if (convertView == null) {
			vi = inflater.inflate(R.layout.menu_item, null);
		}

		TextView description = (TextView) vi.findViewById(R.id.description);
		

		ImageView image = (ImageView) vi.findViewById(R.id.image);

		

		merchandise = data.get(position);

		description.setText(merchandise.get(MenuListView.ITEM_DESCRIPTION));
		

		image.setImageResource(R.drawable.right_arrow);

		return vi;  
    }
    


    @Override
    public View getContentView(int position, View convertView, ViewGroup parent) {

    	View vi = convertView;
		if (convertView == null) {
			vi = inflater.inflate(R.layout.menu_expanded_item, null);
		}
		RelativeLayout rl = (RelativeLayout)vi.findViewById(R.id.layout);
		LinearLayout ll = new LinearLayout(context);
		
		LinearLayout.LayoutParams lllp = new LinearLayout.LayoutParams ( RelativeLayout.LayoutParams.WRAP_CONTENT,
	            RelativeLayout.LayoutParams.WRAP_CONTENT );
		
		int dpValueLeft = 50; // margin in dps
		float d = context.getResources().getDisplayMetrics().density;
		int marginLeft = (int)(dpValueLeft * d); // margin in pixels
		
		int dpValueBottom = 10; // margin in dps
		d = context.getResources().getDisplayMetrics().density;
		int marginBottom = (int)(dpValueBottom * d); // margin in pixels
		
		lllp.setMargins(marginLeft, 0, 0, marginBottom);
		

		
		
		ll.setOrientation(LinearLayout.VERTICAL);
		
		RelativeLayout.LayoutParams rllp = new RelativeLayout.LayoutParams ( RelativeLayout.LayoutParams.WRAP_CONTENT,
	            RelativeLayout.LayoutParams.WRAP_CONTENT );
		
		
		String[] items = merchandise.get(MenuListView.ITEM_NAME).split("~");
		for(int i = 0; i < items.length; i++)
		{
			TextView tv = new TextView(context);
			tv.setId ( i );
			tv.setText(items[i]);
			tv.setTextColor(Color.BLACK);

			

			if ( i > 0 ) {
				rllp.addRule ( RelativeLayout.BELOW, i - 1 );
	        }
			
			tv.setLayoutParams(lllp);
			
			ll.addView(tv);
		}
		ll.setLayoutParams(lllp);
		rl.addView(ll);
		//item.setText(merchandise.get(MenuListView.ITEM_NAME));

        return vi;
    }
}